﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using Utils;
using PanasonicWarranty.DAL;
namespace PanasonicWarranty.Business
{
    public partial class pa_Product
    {
    	#region public properties
		[Required(ErrorMessage="Product Code Required")]
		public string ProductCode { get; set; }
		[Required(ErrorMessage="Product Name Required")]
		public string ProductName { get; set; }
		public string ProductSpecification { get; set; }
		public string CateCode { get; set; }
		public bool? Disabled { get; set; }
		#endregion

		#region constructor
		public pa_Product(){ }

		public pa_Product(IDataReader rd)
		{
			this.ProductCode = rd["ProductCode"].ToString();
			this.ProductName = rd["ProductName"].ToString();
			this.ProductSpecification = rd["ProductSpecification"].ToString();
			this.CateCode = rd["CateCode"].ToString();
			this.Disabled = rd["Disabled"].Equals(DBNull.Value) ? (bool?)null : (bool)rd["Disabled"];
		}
		#endregion

		#region private method
        private bool Create()
        {
            return DBpa_Product.Create(ProductCode, ProductName, ProductSpecification, CateCode, Disabled);
        }

        private bool Update()
        {
            return DBpa_Product.Update(ProductCode, ProductName, ProductSpecification, CateCode, Disabled);
        }
		#endregion

		#region public method
		public bool Save()
		{
            if(ProductCode != string.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
		}
		#endregion

		#region static method
        public static bool Delete(string ProductCode)
        {
            return DBpa_Product.Delete(ProductCode);
        }

        public static List<pa_Product> GetAll()
        {
            return ObjectClass.CreateList<pa_Product>(DBpa_Product.GetAll());
        }

        public static pa_Product GetByKey(string ProductCode)
        {
            return ObjectClass.CreateInstance<pa_Product>(DBpa_Product.GetByKey(ProductCode));
        }

        public static List<pa_Product> GetByForeignKeys(string CateCode)
        {
            return ObjectClass.CreateList<pa_Product>(DBpa_Product.GetByForeignKeys(CateCode));
        }

        public static List<pa_Product> GetByTop(string top, string where, string order)
        {
            return ObjectClass.CreateList<pa_Product>(DBpa_Product.GetByTop(top, where, order));
        }
		#endregion
	}
}
